package com.learning.JavaSE.workExercise.module3.Task3;
/**
 * 分析以下需求，并用代码实现：
 * (1)定义一个学生类Student，包含属性：姓名(String name)、年龄(int age)
 * (2)定义Map集合，用Student对象作为key，用字符串(此表示表示学生的住址)作为value
 * (3)利用四种方式遍历Map集合中的内容，格式：key::value
 */
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class StudentTest {
    public static void main(String[] args) {
        //(2)定义Map集合，用Student对象作为key，用字符串(此表示表示学生的住址)作为value
        Map<Student,String> studentMap = new HashMap<>();
        studentMap.put(new Student("张三", 20), "北京");
        studentMap.put(new Student("李四", 20), "南京");
        studentMap.put(new Student("王五", 20), "上海");
        studentMap.put(new Student("赵六", 20), "广州");
        studentMap.put(new Student("孙七", 20), "深圳");
        //(3)利用四种方式遍历Map集合中的内容，格式：key::value
        // 遍历方式一:增强for循环(entrySet)
        for (Map.Entry<Student,String> entry: studentMap.entrySet()) {
            System.out.println(entry.getKey() + "::" + entry.getValue());
        }

        //遍历方式二:增强for循环(keySet)
        System.out.println("-----------------------------------------");
        for(Student student :studentMap.keySet()) {
            String address = studentMap.get(student);
            System.out.println(student+"::"+address);
        }

        // 遍历方式三:迭代器(通过entrySet())
        System.out.println("-----------------------------------------");
        Set<Map.Entry<Student, String>> set = studentMap.entrySet();
        Iterator<Map.Entry<Student, String>> it = set.iterator();
        while (it.hasNext()) {
            Map.Entry<Student, String> entry = it.next();
            System.out.println(entry.getKey() + "::" + entry.getValue());
        }

        // 遍历方式四:迭代器(通过keySet())
        System.out.println("-----------------------------------------");
        Set<Student> keySet = studentMap.keySet();
        Iterator<Student> it2 = keySet.iterator();
        while(it2.hasNext()) {
            Student student = it2.next();
            String address = studentMap.get(student);
            System.out.println(student+"::"+address);
        }
    }
}
